package com.ft.f_springboot_route.controller;



import com.ft.f_springboot_route.bean.Banner;
import com.ft.f_springboot_route.bean.Route;
import com.ft.f_springboot_route.service.BannerService;
import com.ft.f_springboot_route.service.RouteService;
import com.ft.f_springboot_route.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class BannerContorller {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private RouteService routeService;


    @RequestMapping("toBannerList")
    public String toBannerList(Integer routeId, Integer current, Model model){
        System.out.println(routeId);
        Integer currentPage = 1;
        if(current!=null){
            currentPage=current;
            model.addAttribute("currentPage", currentPage);
        }
        if(routeId!=null){
            model.addAttribute("routeId",routeId);
        }
        Pager<Banner> bannerPager = bannerService.selectBannerAll(routeId, currentPage);
        List<Banner> list =bannerPager.getList();
        for (Banner banner : list) {
            System.out.println(banner);
        }
        model.addAttribute("Pager", bannerPager);
        return "banner/bannerlist";

    }

    @RequestMapping("toBannerAdd")
    public String toAdd(Model model){
        List<Route> routes = routeService.toRouteList();
        model.addAttribute("routesList",routes);
        return "banner/addbanner";

    }

    @RequestMapping("addBanner")
    public String addBanner(Banner banner, @RequestParam("BanSrch") MultipartFile BanSrch){
        Banner banner1=new Banner();
        if(banner!=null){
            banner1.setIsShow(banner.getIsShow());
            banner1.setRouteId(banner.getRouteId());
        }


        String originalFilename = BanSrch.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        String  uploadName=uuid+substring;

        File file=new File("e:/uploads/banner/"+uploadName);
        if (!file.exists()){
            file.mkdirs();
        }
        try {
            BanSrch.transferTo(file);
            banner1.setBanSrc("banner/"+uploadName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bannerService.addBanner(banner1);
        return "redirect:/banner/toBannerList";
    }
    @RequestMapping("deleteBanner")
    public String deleteBanner(Integer banId){

        bannerService.deleteBannerById(banId);

        return "redirect:/banner/toBannerList";
    }

    @RequestMapping("toBannerEdit")
    public String toBannerEdit(Integer banId, Model model, HttpSession session){
        Banner banner = bannerService.selectBannerById(banId);

        List<Route> routes = routeService.toRouteList();
        model.addAttribute("routesList", routes);

        model.addAttribute("Banner", banner);


        session.setAttribute("banner",banner);
        return "banner/setbanner";
    }

    @RequestMapping("editBanner")
    public String editBanner(Banner banner, @RequestParam("BanSrch")MultipartFile BanSrch,HttpSession session){
        Banner bannerId =(Banner) session.getAttribute("banner");
        Banner banner1=new Banner();

        String originalFilename = BanSrch.getOriginalFilename();
        if(BanSrch.getSize()==0){
            bannerId.setIsShow(banner.getIsShow());
            bannerId.setRouteId(banner.getRouteId());
            bannerService.editBannerById(bannerId);
        }else{
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            File delefile=new File("e:/uploads/"+bannerId.getBanSrc());
            if(delefile.exists()){
                delefile.delete();
            }


            String  uploadName=uuid+substring;
            System.out.println(uploadName+"qqqqqqqqqqqqqqqqqqqqqqqqqqqq");
            File file=new File("e:/uploads/banner/"+uploadName);
            if (!file.exists()){
                file.mkdirs();
            }
            banner1.setBanId(banner.getBanId());
            banner1.setIsShow(banner.getIsShow());
            banner1.setRouteId(banner.getRouteId());
            try {
                BanSrch.transferTo(file);
                banner1.setBanSrc("banner/"+uploadName);
                System.out.println(banner1);
                bannerService.editBannerById(banner1);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return "redirect:/banner/toBannerList";
    }
}

